package com.heimdallr.util;


import org.nustaq.serialization.FSTConfiguration;

import java.io.*;

/**
 *
 * @author Panxl
 * @date 2017/6/20
 */
public class SerializationUtils {

    public static String charsetCode = "ISO-8859-1";

    public SerializationUtils() {
    }

    static FSTConfiguration configuration = FSTConfiguration
            // .createDefaultConfiguration();
            .createStructConfiguration();

    public static byte[] serialize(Object obj) {
        return configuration.asByteArray(obj);
    }

    public static Object deserialize(byte[] sec) {
        return configuration.asObject(sec);
    }

    /**
     * jdk原生序列化方案
     * @param obj
     * @return
     */
    public static byte[] jdkserialize(Object obj) {
        try{
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            return baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static Object jdkdeserialize(byte[] bits) {
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bits);
             ObjectInputStream ois = new ObjectInputStream(bais);

        ) {
            return ois.readObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
